/**
 * @Copyright: Interactive Business Solution Pte Ltd, Singapore
 * @Author: Li Wen, 2007-12-21
 * @Version: 071221.001
 * System database access object class
 */
package com.ibsol.database
{
	import com.ibsol.model.SystemEnv;
	
	import flash.data.SQLConnection;
	import flash.data.SQLStatement;
	import flash.events.SQLErrorEvent;
	import flash.events.SQLEvent;
	
	import mx.collections.ArrayCollection;
	import mx.controls.Alert;
	
	public class SystemDAO
	{
		private var _tourixDBConn:SQLConnection;
		
		public function SystemDAO()
		{
			_tourixDBConn = DBConnManager.getConnection("tourix");
		}
		/**
		 * Loads system environment, since there is only one record in the system
		 * @param:	resultHandler:Function		result functioon adapter, return systemEnv object
		 * 			faultHandler:Function		fault function adapter
		 * @return:	void
		 */
		public function loadSystemEnv(resultHandler:Function, faultHandler:Function = null):void
		{
			var sqlStr:SQLStatement = new SQLStatement();
			sqlStr.sqlConnection = _tourixDBConn;
			sqlStr.text = "SELECT s.id, s.serverAddr, s.xmlTree, s.updatetime, s.uid, u.acc, u.pwd, u.level, u.photo " + 
					"FROM system AS s, user AS u WHERE s.uid = u.id";
			sqlStr.addEventListener(SQLEvent.RESULT,
				function(event:SQLEvent):void
				{
					resultHandler.call(this, typeObject(sqlStr.getResult().data[0]));
				});
			sqlStr.addEventListener(SQLErrorEvent.ERROR, faultHandler == null ? sqlErrorHandler : faultHandler);
			sqlStr.execute();
		}
		/**
		 * Updates treeXML into system database. The system only gets one record, id = 1.
		 * @param:	xml:String					xml string, which will be updated
		 * 			resultHandler:Function		result handler function adapter
		 * 			faultHandler:Function		fault handler function adapter
		 * @return:	void
		 */
		public function updateSystemXML(xml:String, resultHandler:Function = null, faultHandler:Function = null):void
		{
			var sqlStr:SQLStatement = new SQLStatement();
			sqlStr.sqlConnection = _tourixDBConn;
			sqlStr.text = "UPDATE system SET xmlTree='" + xml + "' WHERE id=1";
			sqlStr.addEventListener(SQLEvent.RESULT,
				function(event:SQLEvent):void
				{
					if(resultHandler != null)
						resultHandler.call(this);
				});
			sqlStr.addEventListener(SQLErrorEvent.ERROR, faultHandler == null ? sqlErrorHandler : faultHandler);
			sqlStr.execute();
		}
		/**
		 * Clear user information in system table
		 */
		public function resetUser(faultHandler:Function = null):void
		{
			var sqlStr:SQLStatement = new SQLStatement();
			sqlStr.sqlConnection = _tourixDBConn;
			sqlStr.text = "UPDATE system SET uid = 0 WHERE id = 1";
			sqlStr.addEventListener(SQLErrorEvent.ERROR, faultHandler == null ? sqlErrorHandler : faultHandler);
			sqlStr.execute();
		}
		/**
		 * Set user information in system table
		 */
		public function setUser(userid:uint, resultHandler:Function = null, faultHandler:Function = null):void
		{
			var sqlStr:SQLStatement = new SQLStatement();
			sqlStr.sqlConnection = _tourixDBConn;
			sqlStr.text = "UPDATE system SET uid = " + userid + " WHERE id = 1";
			sqlStr.addEventListener(SQLErrorEvent.ERROR, faultHandler == null ? sqlErrorHandler : faultHandler);
			sqlStr.execute();
		}
		/**
		 * Converts database object to systemEnv class
		 * @param:	o:Object database return object
		 * @return:	systemEnv object after conversion from database type
		 */
		public function typeObject(o:Object):SystemEnv
		{
			var s:SystemEnv = new SystemEnv();
			s.serverAddr = o.serverAddr;
			s.uid = o.uid;
			s.acc = o.acc;
			s.pwd = o.pwd;
			s.level = o.level;
			s.photo = o.photo;
			s.updateTime = o.updateTime;
			s.xmlTree = o.xmlTree;
			s.id = o.id;
			return s;
		}
		/**
		 * Converts array object to systemEnv array collection
		 * @param: a:Array array of objects returned by SQL
		 * @return: ArrayCollection ArrayCollection of systemEnv after conversion from Array
		 */
		private function typeArray(a:Array):ArrayCollection
		{
			if (a==null) return null;
			var ac:ArrayCollection = new ArrayCollection();
			for (var i:int; i<a.length; i++)
			{
				ac.addItem(typeObject(a[i]));
			}
			return ac;
		}
		/**
		 * Default SQL error handler, if above functions' fault handlers are null, this error handler is applied
		 * @param: event:SQLError	SQL Error event will be handled if there is an error
		 * @return: void
		 */
		private function sqlErrorHandler(event:SQLErrorEvent):void
		{
			trace(event.text);
			return;
		}
	}
}